﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Cms.Domain;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cms.Tests.Unit.Domain
{

    [TestClass]
    public class ContentTests
    {

        [TestMethod]
        public void GetPublishedHtmlAndTitle_LocaleExists_Success()
        {
            Content c = new Content();
            Locale locale = new Locale { ID = 1 };
            Administrator admin = new Administrator() { ID = 1 };
            ContentTranslationVersion ctv = c.AddVersion(locale, DateTime.Now.Ticks, "some html", "title", admin);
            ctv.Translation.PublishedVersion = ctv;
            Assert.AreEqual("some html", c.GetPublishedHtml(1));
            Assert.AreEqual("title", c.GetPublishedTitle(1));
        }


        [TestMethod]
        public void GetPublishedHtmlAndTitle_LocaleDoesntExist_Success()
        {
            Content c = new Content();
            Locale locale = new Locale { ID = 1 };
            Administrator admin = new Administrator() { ID = 1 };
            ContentTranslationVersion ctv = c.AddVersion(locale, DateTime.Now.Ticks, "some html", "title", admin);
            ctv.Translation.PublishedVersion = ctv;
            Assert.AreEqual("", c.GetPublishedHtml(666));
            Assert.AreEqual("", c.GetPublishedTitle(666));
        }


        [TestMethod]
        public void AddVersion_Success()
        {
            Content c = new Content();
            Locale locale = new Locale { ID = 1 };
            Administrator admin = new Administrator() { ID = 1 };
            Assert.AreEqual(0, c.Translations.Count); // No translations as default
            long timestamp = DateTime.Now.Ticks;
            ContentTranslationVersion ctv = c.AddVersion(locale, timestamp, "some html", "title", admin);
            Assert.AreEqual(1, c.Translations.Count); // One translation should be created
            Assert.AreEqual(1, c.Translations[1].Versions.Count); // ...and one version
            Assert.AreEqual(timestamp.ToString(), c.GetTranslation(1).Versions[0].Timestamp);
            Assert.AreEqual("some html", c.GetTranslation(1).Versions[0].Html);
            Assert.AreEqual("title", c.GetTranslation(1).Versions[0].Title);
            Assert.AreSame(admin, c.GetTranslation(1).Versions[0].Author);
            // Add another version to the same translation
            c.AddVersion(locale, timestamp, "some more html", "title", admin);
            Assert.AreEqual(1, c.Translations.Count); // There should still ne only one translation
        }


        [TestMethod]
        public void GetTranslation_TranslationDoesntExist_NewTranslation()
        {
            Content c = new Content();
            Assert.IsInstanceOfType(c.GetTranslation(666), typeof(ContentTranslation));
            Assert.AreEqual(0, c.GetTranslation(666).ID);
        }


        [TestMethod]
        public void GetTranslation_TranslationExists_ExistingTranslation()
        {
            Content c = new Content();
            ContentTranslationVersion ctv = c.AddVersion(new Locale {ID = 1}, DateTime.Now.Ticks, "some html", "title", new Administrator {ID = 1});
            ContentTranslation translation = ctv.Translation;
            Assert.AreSame(translation, c.GetTranslation(1));
        }


        [TestMethod]
        public void GetContainers_OneOfEachType_Count3()
        {
            Content c = new Content();
            Page p = new Page();
            MasterTemplate mt = new MasterTemplate();
            DetailTemplate dt = new DetailTemplate();
            PageContentLink pageLink = new PageContentLink
            {
                Content = c,
                Page = p,
                AreaID = "pcMain"
            };
            c.Pages.Add(pageLink);
            MasterTemplateContentLink masterTemplateContentLink = new MasterTemplateContentLink
            {
                Content = c,
                MasterTemplate = mt,
                AreaID = "mtcHeader"
            };
            c.MasterTemplates.Add(masterTemplateContentLink);
            DetailTemplateContentLink detailTemplateContentLink = new DetailTemplateContentLink
            {
                Content = c,
                DetailTemplate = dt,
                AreaID = "dtcTop"
            };
            c.DetailTemplates.Add(detailTemplateContentLink);
            Assert.AreEqual(3, c.GetContainers().Count);
        }


        [TestMethod]
        public void RemovePage_TwoAssociations_Success()
        {
            Content c = new Content();
            Page p1 = new Page { ID = 1 };
            PageContentLink pageLink1 = new PageContentLink
            {
                Content = c,
                Page = p1,
                AreaID = "pcMain"
            };
            c.Pages.Add(pageLink1);
            Page p2 = new Page { ID = 2 };
            PageContentLink pageLink2 = new PageContentLink
            {
                Content = c,
                Page = p2,
                AreaID = "pcMain"
            };
            c.Pages.Add(pageLink2);
            Assert.AreEqual(2, c.Pages.Count);
            c.RemovePage(p1);
            Assert.AreEqual(1, c.Pages.Count);
            Assert.AreSame(p2, c.Pages[0].Page);
        }


        [TestMethod]
        public void RemoveMasterTemplate_TwoAssociations_Success()
        {
            Content c = new Content();
            MasterTemplate mt1 = new MasterTemplate { ID = 1 };
            MasterTemplateContentLink mtLink1 = new MasterTemplateContentLink
            {
                Content = c,
                MasterTemplate = mt1,
                AreaID = "pcMain"
            };
            c.MasterTemplates.Add(mtLink1);
            MasterTemplate mt2 = new MasterTemplate { ID = 2 };
            MasterTemplateContentLink mtLink2 = new MasterTemplateContentLink
            {
                Content = c,
                MasterTemplate = mt2,
                AreaID = "pcMain"
            };
            c.MasterTemplates.Add(mtLink2);
            Assert.AreEqual(2, c.MasterTemplates.Count);
            c.RemoveMasterTemplate(mt1);
            Assert.AreEqual(1, c.MasterTemplates.Count);
            Assert.AreSame(mt2, c.MasterTemplates[0].MasterTemplate);
        }


        [TestMethod]
        public void RemoveDetailTemplate_TwoAssociations_Success()
        {
            Content c = new Content();
            DetailTemplate dt1 = new DetailTemplate { ID = 1 };
            DetailTemplateContentLink dtLink1 = new DetailTemplateContentLink
            {
                Content = c,
                DetailTemplate = dt1,
                AreaID = "pcMain"
            };
            c.DetailTemplates.Add(dtLink1);
            DetailTemplate dt2 = new DetailTemplate { ID = 2 };
            DetailTemplateContentLink dtLink2 = new DetailTemplateContentLink
            {
                Content = c,
                DetailTemplate = dt2,
                AreaID = "pcMain"
            };
            c.DetailTemplates.Add(dtLink2);
            Assert.AreEqual(2, c.DetailTemplates.Count);
            c.RemoveDetailTemplate(dt1);
            Assert.AreEqual(1, c.DetailTemplates.Count);
            Assert.AreSame(dt2, c.DetailTemplates[0].DetailTemplate);
        }

        [TestMethod]
        public void IsInCategory_NotInCategory_ReturnFalse()
        {
            Content c = new Content();
            Category category1 = new Category() { ID = 1 };
            c.Categories.Add(category1);
            category1.Content.Add(c);
            Assert.IsFalse(c.IsInCategory(2));
        }

        [TestMethod]
        public void IsInCategory_ContentBelongsToCategory_ReturnTrue()
        {
            Content c = new Content();
            Category category1 = new Category() { ID = 1 };
            c.Categories.Add(category1);
            category1.Content.Add(c);
            Assert.IsTrue(c.IsInCategory(1));
        }

        [TestMethod]
        public void ContainsPageBreak_No_ReturnFalse()
        {
            string html = "<p>Yada yada yada.</p>";
            Assert.IsFalse(Content.ContainsPageBreak(html));
        }

        [TestMethod]
        public void ContainsPageBreak_Yes_ReturnTrue()
        {
            string html = "<p>Yada yada yada.<!-- pagebreak --> Yada yada yada</p>";
            Assert.IsTrue(Content.ContainsPageBreak(html));
        }

        [TestMethod]
        public void TruncateAfterPageBreak_PageBreakPresent_ContentIsTruncatedAndParagraphsAreBeingBalanced()
        {
            string html = "<p>Yada yada yada.<!-- pagebreak --> Yada yada yada</p>";
            Assert.AreEqual("<p>Yada yada yada.</p>", Content.TruncateAfterPageBreak(html));
            html = "<p class=\"xxx\">This is paragraph one.</p><!-- pagebreak --><p class=\"yyy\" style=\"margin-bottom: 0px;\">And this is number two.</p>";
            Assert.AreEqual("<p class=\"xxx\">This is paragraph one.</p>", Content.TruncateAfterPageBreak(html));
            html = "<p class=\"xxx\" onclick=\"doSomething('');\">This is paragraph one.</p><p class=''>xxx<!-- pagebreak --><p class=\"yyy\" style=\"margin-bottom: 0px;\">And this is number two.</p>";
            Assert.AreEqual("<p class=\"xxx\" onclick=\"doSomething('');\">This is paragraph one.</p><p class=''>xxx</p>", Content.TruncateAfterPageBreak(html));
        }
    }
}
